﻿@using Microsoft.EntityFrameworkCore
@using Radzen
@using System.Text.Json
@inject DialogService DialogService
@inject ProductService ProductService
@inject ProductCategoryService ProductCategoryService

<RadzenStack>
    <RadzenRow Gap="1rem">
        <RadzenColumn Size="12" SizeMD="12">
            <RadzenStack>
                <RadzenFormField Text="选中节点名称" Style="width: 100%;">
                    <ChildContent>
                        <RadzenTextBox @bind-Value="PParent!.Name" Disabled="true" ReadOnly="true" />
                    </ChildContent>
                </RadzenFormField>
            </RadzenStack>
        </RadzenColumn>
    </RadzenRow>
</RadzenStack>

<RadzenTemplateForm TItem="ProductCategory" EditContext="editContext" Data="@model" InvalidSubmit=@OnInvalidSubmit Submit=@OnSubmit>
    <DataAnnotationsValidator />
    <ValidationSummary />
    <RadzenStack Gap="1rem">
        <RadzenStack class="rz-p-0" Orientation="Orientation.Vertical" AlignItems="AlignItems.Normal" JustifyContent="JustifyContent.SpaceBetween">
            <RadzenRow Gap="1rem">
                <RadzenColumn Size="12" SizeMD="12">
                    <RadzenStack>
                        <RadzenFormField Text="名称" Style="width: 100%;">
                            <ChildContent>
                                <RadzenTextBox @bind-Value="model!.Name" />
                            </ChildContent>
                            <Helper>
                                <RadzenText TextStyle="TextStyle.Caption" Style="color:red">* 必填</RadzenText>
                            </Helper>
                        </RadzenFormField>
                    </RadzenStack>
                </RadzenColumn>
            </RadzenRow>
        </RadzenStack>
        <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20%;">
            <RadzenButton ButtonType="ButtonType.Button" ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel" />
            <RadzenButton ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Primary" Text="保存" />
        </RadzenStack>
    </RadzenStack>
</RadzenTemplateForm>

@code {
    [Parameter] public ProductCategory? PProductType { get; set; }
    [Parameter] public TreeProductTypeDto? PParent { get; set; }

    private EditContext? editContext;
    private ProductCategory? model { get; set; }

    async void OnSubmit(ProductCategory model)
    {
        if (model == null)
        {
            return;
        }
        if (model.Id == 0)
        {
            model.ParentId = PParent!.Id;
            model.ParentIdChain = PParent!.ParentId + "-" + PParent!.Id;
        }

        if (model.Name == string.Empty)
        {
            await DialogService.Alert("名字不能为空", "错误", new AlertOptions() { OkButtonText = "确定" });
            return;
        }

        if (ProductCategoryService.IsExist(model))
        {
            await DialogService.Alert($"同名 {model.Name} 已经存在。", "错误", new AlertOptions() { OkButtonText = "确定" });
            return;
        }
        var result = ProductCategoryService.Save(model);
        DialogService.Close(result);
    }


    void OnInvalidSubmit(FormInvalidSubmitEventArgs args)
    {
        var errors = new Dictionary<string, List<string>>();
    }

    protected override void OnInitialized()
    {
        model = PProductType;
        editContext = new EditContext(model!);
    }

    private void OnCancel()
    {
        DialogService.Close(null);
    }

}

